<template>
	<view class="study-list" :class="[config.style]">
		<FullscreenLoading :loading="loading" :config="config"></FullscreenLoading>
		<!-- 顶部固定部分 -->
		<view class="top-warp">
			<view class="tabs-block">
				<view class="statistics">
					<view class="item">
						<view class="title">
							学习总时长
						</view>
						<view class="value">
							{{statistics.all_total}}
						</view>
					</view>
					<view class="item">
						<view class="title">
							今日学习时长
						</view>
						<view class="value">
							{{statistics.today_total}}
						</view>
					</view>
					<view class="item">
						<view class="title">
							连续学习天数
						</view>
						<view class="value">
							{{statistics.continue_day}}
						</view>
					</view>
				</view>
				
				<view class="tabs">
					<ListTabs v-model="tabIndex" :config="config" :tabs="tabs" @change="tabChange"></ListTabs>
				</view>
				
			</view>
		</view>

		<!--数据列表部分-->
		<view class="list-section uni-clearfix">
			<!-- top="xxx"下拉布局往下偏移,防止被悬浮菜单遮住 -->
			<mescroll-uni ref="mescrollRef" @init="mescrollInit" :top="topOption" :down="downOption" @down="downCallback" @up="upCallback">
				<!-- 数据列表 -->
				<view class="list">
					<view class="item" v-for="(item,index) in list" :key="index">

						<view class="content">
							<view class="cover">
								<image mode="widthFix" :src="item.order_info.cover_200" @error="imageError" class="image"></image>
								<view class="type">
									{{item.order_info.type_str}}
								</view>
							</view>
							<view class="info">
								<view class="title text-ellipsis-2">{{item.order_info.title}}</view>
								<view class="description text-ellipsis">{{item.order_info.description}}</view>
								
								<view class="start-btn">
									<!--课程-->
									<template v-if="item.order_info_type == 'Knowledge'">
										<button type="default" class="button" hover-class="button-hover" size="mini" @click="goToDetailLink(item.order_info_id,item.order_info_type)">
											开始学习
										</button>
									</template>
									<!--专栏-->
									<template v-if="item.order_info_type == 'KnowledgeColumn'">
										<button type="default" class="button" hover-class="button-hover" size="mini" @click="goToDetailLink(item.order_info_id,item.order_info_type)">
											开始学习
										</button>
									</template>
									<!--线下课-->
									<template v-if="item.order_info_type == 'Offline'">
										<button type="default" class="button" hover-class="button-hover" size="mini" @click="goToDetailLink(item.order_info_id,item.order_info_type)">
											核销课节
										</button>
									</template>
									<!--直播-->
									<template v-if="item.order_info_type == 'Live'">
										<button type="default" class="button" hover-class="button-hover" size="mini" @click="goToDetailLink(item.order_info_id,item.order_info_type)">
											进入直播
										</button>
									</template>
									<!--资料下载-->
									<template v-if="item.order_info_type == 'Material'">
										<button type="default" class="button" hover-class="button-hover" size="mini" @click="goToDetailLink(item.order_info_id,item.order_info_type)">
											立即下载
										</button>
									</template>
									<!--题库考试-->
									<template v-if="item.order_info_type == 'ExamPaper'">
										<button type="default" class="button" hover-class="button-hover" size="mini" @click="goToDetailLink(item.order_info_id,item.order_info_type)">
											打开试卷
										</button>
									</template>
								</view>
							</view>
						</view>
						<view class="pay-info">

						</view>
					</view>
				</view>
			</mescroll-uni>
		</view>
	</view>
</template>

<script>
	import { mapState,mapMutations } from 'vuex'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import ListTabs from "../../common/listTabs"; //列表tab
	//全屏加载中
	import FullscreenLoading from '@/components/muu-fullscreen-loading/muu-fullscreen-loading.vue'

	export default {
		mixins: [MescrollMixin], // 使用mixin (在main.js注册全局组件)
		components: {
			ListTabs,
			FullscreenLoading,
		},
		data() {
			return {
				loading: true, //加载中
				urlParams: {}, //url传递过来的参数
				statistics : {}, //学习统计数据
				btnText: '开始学习',
				pluginList: [],
				tabs: [
					{'name':'全部', 'type': 'all'}, 
					{'name':'课程', 'type': 'Knowledge' },
					{'name':'专栏', 'type': 'Column'},
				],
				type: 'all',
				page: 1, //当前页码
				list: [], //列表数据
				tabIndex: 0 ,// tab下标
				topOption: 168, 
				downOption: {
					auto: false //是否在初始化后,自动执行downCallback; 默认true
				},
				upOption:{
					// page: {
					// 	size: 10 // 每页数据的数量
					// },
					noMoreSize: 4, //如果列表已无数据,可设置列表的总数量要大于半页才显示无更多数据;避免列表数据过少(比如只有一条数据),显示无更多数据会不好看; 默认5
					empty:{
						tip: '~ 搜索无数据 ~', // 提示
						btnText: '去看看'
					}
				},
				liveConfig: {},   // 直播插件配置
				offlineConfig: {}, // 线下课插件配置
				materialConfig: {}, // 资料下载插件配置
				examConfig: {}, // 题库考试插件配置
			}
		},
		async onLoad(option) {
			//获取url参数
			this.urlParams = option
			this.getStatistics()
			await this.getPlugin()
			await Promise.all([
				this.getLivecourseConfig(),
				this.getOfflineConfig(),
				this.getMaterialConfig(),
				this.getExamConfig()
			])
			await this.setTabs()
			this.loading = false
		},
		created() {
			
		},
		computed: {
			...mapState(['config'])
		},
		methods: {

			/**
			 * 学习统计数据
			 */
			getStatistics(){
				let that = this
				let data = {}
					data.action = 'statistics'
					data.m = 'muu_classroom'
				//获取数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/study'
					//#endif
					//#ifdef H5
					url = 'entry/site/study'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							console.log(res.data)
							that.statistics = res.data.data
						}
					}
				})
			},
			
			/**
			 * 获取已安装插件列表
			 */
			getPlugin(){
				let that = this
				//获取分类数据
				return new Promise((resolve, reject) => {
					let data = {}
					let url = '';
						//#ifdef MP-WEIXIN
						url = 'entry/wxapp/plugin'
						//#endif
						//#ifdef H5
						url = 'entry/site/plugin'
						//#endif
					this.util.request({
						url : url,
						data : data,
						success: function (res) {
							if (res.data.code == 200) {
								let plugin = res.data.data
								plugin.forEach(function(v,l){
									that.pluginList.push(v.name)
								})
								console.log(that.pluginList)
							}
							resolve(res.data)
						}
					})
				})
			},
			
			/**
			 * 获取直播插件配置
			 */
			getLivecourseConfig(){
				let that = this
				//获取分类数据
				return new Promise((resolve, reject) => {
					let data = {
						
					}
					let url = '';
						//#ifdef MP-WEIXIN
						url = 'entry/wxapp/livecourse'
						data.action = 'config'
						data.m = 'muu_classroom'
						//#endif
						//#ifdef H5
						url = 'entry/site/config'
						data.m = 'muu_classroom_plugin_livecourse'
						//#endif
					this.util.request({
						
						url : url,
						data : data,
						success: function (res) {
							if (res.data.code == 200) {
								that.liveConfig = res.data.data
							}
							resolve(res.data)
						},
						fail: function(){
							reject()
						}
					})
				})
			},
			
			/**
			 * 获取线下课插件配置
			 */
			getOfflineConfig(){
				let that = this
				//获取分类数据
				return new Promise((resolve, reject) => {
					let data = {
						
					}
					let url = '';
						//#ifdef MP-WEIXIN
						url = 'entry/wxapp/offline'
						data.action = 'config'
						data.m = 'muu_classroom'
						//#endif
						//#ifdef H5
						url = 'entry/site/config'
						data.m = 'muu_classroom_plugin_offline'
						//#endif
					this.util.request({
						
						url : url,
						data : data,
						success: function (res) {
							if (res.data.code == 200) {
								that.offlineConfig = res.data.data
							}
							resolve(res.data)
						},
						fail: function(){
							reject()
						}
					})
				})
			},
			
			/**
			 * 获取资料下载插件配置
			 */
			getMaterialConfig(){
				let that = this
				//获取分类数据
				return new Promise((resolve, reject) => {
					let data = {
						
					}
					let url = '';
						//#ifdef MP-WEIXIN
						url = 'entry/wxapp/material'
						data.action = 'config'
						data.m = 'muu_classroom'
						//#endif
						//#ifdef H5
						url = 'entry/site/config'
						data.m = 'muu_classroom_plugin_material'
						//#endif
					this.util.request({
						
						url : url,
						data : data,
						success: function (res) {
							if (res.data.code == 200) {
								that.materialConfig = res.data.data
							}
							resolve(res.data)
						},
						fail: function(){
							reject()
						}
					})
				})
			},
			
			/**
			 * 获取资料下载插件配置
			 */
			getExamConfig(){
				let that = this
				//获取分类数据
				return new Promise((resolve, reject) => {
					let data = {
						
					}
					let url = '';
						//#ifdef MP-WEIXIN
						url = 'entry/wxapp/exam'
						data.action = 'config'
						data.m = 'muu_classroom'
						//#endif
						//#ifdef H5
						url = 'entry/site/config'
						data.m = 'muu_classroom_plugin_exam'
						//#endif
					this.util.request({
						url : url,
						data : data,
						success: function (res) {
							if (res.data.code == 200) {
								that.examConfig = res.data.data
							}
							resolve(res.data)
						},
						fail: function(){
							reject()
						}
					})
				})
			},
			
			/**
			 * 设置tabs内容
			 */
			setTabs() {
				let that = this
				this.pluginList.forEach(function(v,l){
					if(v == 'muu_classroom_plugin_livecourse' && that.liveConfig.status == 1){
						that.tabs.push({'name': '直播', 'type': 'Live'})
					}
					if(v == 'muu_classroom_plugin_offline' && that.offlineConfig.status == 1){
						that.tabs.push({'name': '线下课', 'type': 'Offline'})
					}
					if(v == 'muu_classroom_plugin_material' && that.materialConfig.status == 1){
						that.tabs.push({'name': '资料',  'type': 'Material'})
					}
					if(v == 'muu_classroom_plugin_exam' && that.examConfig.status == 1){
						that.tabs.push({'name': '试卷', 'type': 'ExamPaper'})
					}
				})
			},
			
			
			
			//上拉加载的回调
			upCallback() {
				let that = this
				let data = this.urlParams
					data.action = 'lists'
					data.m = 'muu_classroom'
					data.page = this.page
					data.paid = 1
					data.type = this.type
				//获取数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/study'
					//#endif
					//#ifdef H5
					url = 'entry/site/study'
					//#endif
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							let list = res.data.data.data
							that.list = that.list.concat(list);
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(list.length);
							that.page = that.page + 1
						}
					}
				})
			},
			//下拉刷新
			downCallback: function() {
				let that = this
				this.page = 1
				let data = this.urlParams
					data.action = 'lists'
					data.m = 'muu_classroom'
					data.page = this.page
					data.paid = 1
					data.type = this.type
				//获取数据
				let url = ''
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/study'
					//#endif
					//#ifdef H5
					url = 'entry/site/study'
					//#endif
				this.util.request({
					url : 'entry/wxapp/study',
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.list = []
							that.list = res.data.data.data
							//联网成功的回调,隐藏下拉刷新和上拉加载的状态;
							that.mescroll.endSuccess(that.list.length);
							that.page = 2
						}
					}
				})
			},
			
			// 切换TAB
			tabChange: function(index) {
				console.log(this.urlParams)
				this.type = this.tabs[index].type
				this.page = 1
				this.list = []// 先置空列表,显示加载进度
				this.mescroll.resetUpScroll() // 再刷新列表数据
			},
			
			/**
			 * 进入内容详情页
			 * @param {Object} id
			 * @param {Object} type
			 */
			goToDetailLink: function(id,type){
				console.log(type)
				let page = ''
				if(type == 'Knowledge'){
					page = '../../knowledge/detail'
				}
				if(type == 'KnowledgeColumn'){
					page = '../../column/detail'
				}
				if(type == 'Live'){
					page = '../../live/detail'
				}
				if(type == 'Offline'){
					page = '../../offline/verification'
				}
				if(type == 'Material'){
					page = '../../material/detail'
				}
				if(type == 'ExamPaper'){
					page = '../../exam/paper/detail'
				}
				let urlParams = 'id='+id
				let url = page + '?' + urlParams
				console.log(url)
				//指定跳转地址
				uni.navigateTo({
				    url: url
				});
			},
		}
	}
</script>

<style lang="scss">
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		background-color: #f8f8f8;
		min-height: 100%;
		height: auto;
	}
	
	/*顶部固定部分*/
	.top-warp {
		background-color: #ffffff;
		.tabs-block {
			margin: 0 20rpx;
			.statistics {
				display: flex;
				padding: 8rpx 0;
				border-bottom: 2rpx solid #f8f8f8;
				.item{
					flex: 1;
					.title {
						text-align: center;
						font-size: 28rpx;
						color: #999;
					}
					.value {
						text-align: center;
						font-size: 30rpx;
						font-weight: 700;
					}
				}
			}
			
			.tabs {
				
			}
		}
	}
	.list {
		/*小图*/
		padding: 0 10rpx;
		.item {
			box-sizing:border-box;
			background-color: #fff;
			margin: 10rpx;
			border-radius: 10rpx;
			box-shadow: 0 0 10rpx rgba(0,0,0,.02);
			overflow: hidden;
			.content {
				display: flex;
				padding: 20rpx;
				.cover {
					position: relative;
					width: 280rpx;
					overflow: hidden;
					.image {
						border-radius: 10rpx;
						width: 100%;
						height: auto;
						display: block;
					}
					.type {
						position:absolute;
						bottom: 10rpx;
						right: 10rpx;
						background-color:rgba(0,0,0,.5);
						color:#fff;
						padding:4rpx 10rpx;
						border-radius: 10rpx;
						font-size: 24rpx;
					}
				}
				.info {
					flex: 1;
					padding: 0 0 0 20rpx;
					.title {
						font-size: 28rpx;
						color: #000;
						font-weight: 600;
						height: 72rpx;
						line-height: 36rpx;
					}
					.description {
						color: #999;
						font-size: 24rpx;
					}
					.start-btn {
						margin-top: 20rpx;
						font-size: 28rpx;
						text-align: right;
						.button {
							color: #fff;
						}
					}
					
				}
			}
		}
	}
	
	.Blue {
		.price {
			color: $Blue;
		}
		.start-btn {
			.button {
				background-color: $Blue;
			}
			.button-hover {
				background-color: $Blue-hover;
			}
		}
	}
	.Green {
		.price {
			color: $Green;
		}
		.start-btn {
			.button {
				background-color: $Green;
			}
			.button-hover {
				background-color: $Green-hover;
			}
		}
	}
	.LightRed {
		.price {
			color: $LightRed;
		}
		.start-btn {
			.button {
				background-color: $LightRed;
			}
			.button-hover {
				background-color: $LightRed-hover;
			}
		}
	}
	.Orange {
		.price {
			color: $Orange;
		}
		.start-btn {
			.button {
				background-color: $Orange;
			}
			.button-hover {
				background-color: $Orange-hover;
			}
		}
	}
	.LightPink {
		.price {
			color: $LightPink;
		}
		.start-btn {
			.button {
				background-color: $LightPink;
			}
			.button-hover {
				background-color: $LightPink-hover;
			}
		}
	}
	.Magenta {
		.price {
			color: $Magenta;
		}
		.start-btn {
			.button {
				background-color: $Magenta;
			}
			.button-hover {
				background-color: $Magenta-hover;
			}
		}
	}
</style>
